package com.yc.music.common.util;

import java.io.IOException;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * 
* @ClassName: QiNiuOperator 
* @Description: 七牛工具类
* @author qinhaiguan 
* @date 2016年6月16日 上午11:34:40 
* @version
*
 */
public class QiNiuOperator {
	
	// 设置好账号的ACCESS_KEY和SECRET_KEY
	private final static String ACCESS_KEY = "nUdQcDzxiYQzP9tClx85tec8GWq7ZMMqvqufWd8f";
	private final static String SECRET_KEY = "lo0yDnptRcIhWZysqHdIFmSbcPfGf3BZCHmh7Ek7";
	// 要上传的空间
	private final static Auth auth;
	static {
		auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	}

	/**
	 * 生成上传 Token getUpToken(这里用一句话描述这个方法的作用)
	 *
	 * @Title: getUpToken @Description: TODO @param bucketname 要上传的空间名称 @return
	 * String 返回类型 @throws
	 */
	public static String getUpToken(String bucketname) {
		return auth.uploadToken(bucketname);
	}

	private static UploadManager uploadManager = new UploadManager();

	/**
	 * 
	 * upload:(七牛文件上传). <br/> 
	 *
	 * @author panguixiang
	 * @param path  原文件路径
	 * @param bucketname 空间名称
	 * @param key 保存到七牛上的名字全称
	 * @throws IOException
	 * @throws QiniuException
	 * @since JDK 1.7
	 */
	public static void uploadQiNiu(String path, String bucketname, String key) throws IOException, QiniuException {
		// 调用put方法上传
		 uploadManager.put(path, key, getUpToken(bucketname));
	}
	
	/**
	 * 
	 * uploadQiNiuByByte:(直接上传文件字节码到七牛). <br/> 
	 *
	 * @author panguixiang
	 * @param bytearr
	 * @param bucketname
	 * @param key
	 * @throws IOException
	 * @throws QiniuException
	 * @since JDK 1.7
	 */
	public static void uploadQiNiuByByte(byte[] bytearr, String bucketname, String key) throws IOException, QiniuException {
		// 调用put方法上传
		uploadManager.put(bytearr, key, getUpToken(bucketname));
	}

	/**
	 * 删除操作
	 * deleteByKey:(这里用一句话描述这个方法的作用). <br/> 
	 *
	 * @author panguixiang
	 * @param key
	 * @param bucket
	 * @throws QiniuException 
	 * @since JDK 1.7
	 */
    //实例化一个BucketManager对象
   private static BucketManager bucketManager = new BucketManager(auth);
	public static void deleteByKey(String key,String bucket) {
        //调用delete方法移动文件
        try {
			bucketManager.delete(bucket, key);
		} catch (QiniuException e) {
		}
	}
	      
	public static void main(String args[]) {
		String key="app_config.txt";
		String buck="uploadfiles";
			deleteByKey(key,buck);
	}

}
